package com.jankins.pat;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Scanner;

public class StringMatcher {
    public static Map<Character, String> week = new HashMap<>();
    public static Map<Character, String> hour = new HashMap<>();

    static {
        // The week mapping rules
        week.put('A', "MON");
        week.put('B', "TUE");
        week.put('C', "WED");
        week.put('D', "THU");
        week.put('E', "FRI");
        week.put('F', "SAT");
        week.put('G', "SUN");

        // The hour mapping rules
        hour.put('0', "0");
        hour.put('1', "1");
        hour.put('2', "2");
        hour.put('3', "3");
        hour.put('4', "4");
        hour.put('5', "5");
        hour.put('6', "6");
        hour.put('7', "7");
        hour.put('8', "8");
        hour.put('9', "9");
        hour.put('A', "10");
        hour.put('B', "11");
        hour.put('C', "12");
        hour.put('D', "13");
        hour.put('E', "14");
        hour.put('F', "15");
        hour.put('G', "16");
        hour.put('H', "17");
        hour.put('I', "18");
        hour.put('J', "19");
        hour.put('K', "20");
        hour.put('L', "21");
        hour.put('M', "22");
        hour.put('N', "23");
    }

    public static void main(String[] args) {
        Scanner in = new Scanner(System.in);

        while(in.hasNext()) {
            String[] strings = new String[4];

            for(int i = 0; i < 4; i++) {
                strings[i] = in.next();
            }

            List<String> result = new ArrayList<>();
            
            matches(strings[0], strings[1], result);
            matches(strings[2], strings[3], result);

            // format the output for the result
            System.out.println(result.get(0) + " "
                                + result.get(1) + ":"
                                + result.get(2));
        }
    }

    public static void matches(String text, String pattern, List<String> result) {
        if(result.size() > 3) 
            throw new IndexOutOfBoundsException();

        int len = Math.min(text.length(), pattern.length());

        // the flag status.
        boolean w = result.size() == 0 ? false : true;
        boolean h = result.size() < 2 ? false : true;
        boolean m = result.size() < 3 ? false : true;

        for(int i = 0; i < len; i++) {
            if(w && h && m) return;

            char tc = text.charAt(i);
            char pc = pattern.charAt(i);

            if(tc != pc) continue;

            // processing the week
            if(!w) {
                String wk = week.get(tc);
                if(wk != null) {
                    w = true;
                    result.add(wk);
                }
                continue;
            }

            // processing the hour
            if(!h) {
                String hr = hour.get(tc);
                if(hr != null) {
                    h = true;
                    result.add(hr);
                }
                continue;
            }

            // processing the minute
            if(!m) {
                if((tc >= 'a' && tc <= 'z') || (tc >= 'A' && tc <= 'Z')) {
                    String mins = (i > 9) ? ("" + i)
                                          : ("0" + i);
                    result.add(mins);
                    m = true;
                    continue;
                }
                continue;
            }
        }
    }
}
